package com.car.action.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;
import com.stonehenge.backend.tools.StaticToolit;
import com.stonehenge.backend.tools.data.DataFactory;
import com.utils.Pic2Util;
import com.utils.PicUtil;
import com.utils.UuidUtil;


public class UtilController extends Controller {
	/*
	 * 上传文件
	 */
	public void uploadFile() {
		JSONObject json = new JSONObject();
		String fileName ="";
		try {
//			System.out.println(PathKit.getWebRootPath());
//			UploadFile file = getFile("pic", PathKit.getWebRootPath() + "\\temp");
//			File source = file.getFile();
			UploadFile file = getFile(); 
			File source = file.getFile();
			fileName = file.getFileName();
			// 后缀
			String extension = "";
			if(fileName.lastIndexOf(".") > 0){
				extension = fileName.substring(fileName.lastIndexOf("."));
			}
			fileName = "java_admin_"+UuidUtil.get32Id() + extension;
			new PicUtil().upload(source.getAbsolutePath(), fileName);
			source.deleteOnExit();
		} catch (FileNotFoundException e) {
			json.put("error", 1);
			json.put("message", "上传出现错误，请稍后再上传");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("error", 1);
			json.put("message", "文件写入服务器出现错误，请稍后再上传");
		}
		json.put("error", 0);
		json.put("url", fileName);
		renderJson(json);
	}
	
	/**
	 * 生成resid
	 * 
	 * @param map
	 * @return
	 */
	private String genResID(Map<String, Object> map) {
		String imagetype = (String) map.get("imagetype");
		String usingdir = (String) map.get("usingdir");
		long userid = 61034864334556L;
		String format = (String) map.get("format");
		if (StaticToolit.isEmpty(imagetype) || StaticToolit.isEmpty(usingdir)
				|| StaticToolit.isEmpty(format))
			return null;
		String resid = "";
		String fileName = DataFactory.newFileID(String.valueOf(userid),
				imagetype, usingdir); // 不包括后缀
		String fileFullName = fileName + "." + format; // 包括后缀
		if (!resid.isEmpty()) {// 如果已经设定
			if (resid.contains(".")) {
				fileName = resid.substring(0, resid.indexOf("."));
				fileFullName = resid;
			} else {
				fileFullName = resid + "." + format;
				fileName = resid;
			}
		}
		resid = fileFullName;// 确保带后缀
		return resid;
	}
	
	
	/*
	 * 上传文件
	 */
	public void uploadFile2() {
		JSONObject json = new JSONObject();
		String fileName ="";
		try {
			System.out.println(PathKit.getWebRootPath());
			UploadFile file = getFile(); 
			File source = file.getFile();
			fileName = file.getFileName();
			// 后缀
			String extension = "";
			if(fileName.lastIndexOf(".") > 0){
				extension = fileName.substring(fileName.lastIndexOf("."));
			}
			fileName = "shihou"+UuidUtil.get32Id() + extension;
			new Pic2Util().upload(source.getAbsolutePath(), fileName);
//			target.deleteOnExit();
		} catch (FileNotFoundException e) {
			json.put("error", 1);
			json.put("message", "上传出现错误，请稍后再上传");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("error", 1);
			json.put("message", "文件写入服务器出现错误，请稍后再上传");
		}
		json.put("error", 0);
		json.put("url", fileName);
		renderJson(json);
	}

	public static void main(String[] args) {
		
		  Pattern r = Pattern.compile("[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} WARN");
	      Matcher m = r.matcher("17:12:08,552 WARN  - Unrecogniced VR code: {0}H - treat as UN");
//	      System.out.println("ReplaceAll:" + m.replaceAll("favour"));
	     System.out.println("17:12:08,552 WARN  - Unrecogniced VR code: {0}H - treat as UN".replaceAll("[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} WARN", ""));
	        
	}
}
